package com.orange.studio.dbk.fragments;


import java.util.List;

import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.meta.gui.quickaction.ActionItem;
import com.meta.gui.quickaction.QuickAction;
import com.orange.studio.dbk.R;
import com.orange.studio.dbk.adapters.ListFavioritesSongAdapter;
import com.orange.studio.dbk.dao.FavioritesDAO;
import com.orange.studio.dbk.dao.SongDAO;
import com.orange.studio.dbk.dto.SearchDTO;
import com.orange.studio.dbk.dto.SongDTO;
import com.orange.studio.dbk.utils.AppConfig;
import com.orange.studio.dbk.utils.AppConfig.SEARCH_TYPE;
import com.orange.studio.dbk.utils.DBKUtils;
import com.orange.studio.uicore.customviews.LoadMoreListViewUICore;

public class FavioritesFragment extends BaseFragment {
	private ListFavioritesSongAdapter mAdapter;
	private SongDAO mSongDAO;
	
	private GetSongFromStore mGetSongFromStore=null;
	private View mVolNumber=null;
	private View mSearchToolbar=null;
	
	private TextView mTypeNumber=null;
	private QuickAction menu=null;
	private ImageView mSubmitSearch=null;
	private TextView mSearchText=null;
	private SearchDTO search=null;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (mView == null) {
			mView = inflater.inflate(R.layout.fragment_layout_home,
					container, false);			
			initView();
			initListener();
			createPopupMenu();
		} else {
			((ViewGroup) mView.getParent()).removeView(mView);
		}
		return mView;
	}
	private void initView(){
		
		initNotFoundAndLoadingView();
		mVolNumber=(LinearLayout)mView.findViewById(R.id.menuTypeNumber);
		mTypeNumber=(TextView)mView.findViewById(R.id.currentTypeNumber);
		mTypeNumber.setText(getActivity().getString(R.string.type_5_number));
		mSubmitSearch=(ImageView)mView.findViewById(R.id.submitSearch);
		mSearchText=(TextView)mView.findViewById(R.id.searchText);
		mSearchToolbar=(LinearLayout)mView.findViewById(R.id.searchToolbar);
		mSearchToolbar.setVisibility(View.GONE);
		mSongDAO=new SongDAO(getActivity());
		
		mListView=(LoadMoreListViewUICore)mView.findViewById(R.id.mListView);		
		mAdapter=new ListFavioritesSongAdapter(getActivity(),this);
		mListView.setAdapter(mAdapter);
		
		search=new SearchDTO();
		search.language=1;
		
	}
	private void initListener(){
		initRetryButtonListener();
		mVolNumber.setOnClickListener(this);
		mSubmitSearch.setOnClickListener(this);
		mListView.setOnLoadMoreListener(this);
	}
	private void createPopupMenu(){
		menu=new QuickAction(getActivity());
		menu.setOnActionItemClickListener(this);
		ActionItem item1=new ActionItem(111, getActivity().getString(R.string.type_5_number));
		ActionItem item2=new ActionItem(112, getActivity().getString(R.string.type_6_number));
		
		
		menu.addActionItem(item1);
		menu.addActionItem(item2);		
		
	}
	private void showPopupMenu(View v) {		
		menu.show(v);
	}
	@Override
	public void onClick(View v) {
		int id=v.getId();
		switch (id) {
		case R.id.menuTypeNumber:
			showPopupMenu(mVolNumber);
			break;
		case R.id.submitSearch:
			mAdapter.cleanListData();
			pageNo=1;
			search.type=SEARCH_TYPE.SEARCH_TYPE_SONG_NAME;
			getData();
			break;
		default:
			super.onClick(v);
			break;
		}		
	}
	@Override
	public void onItemClick(QuickAction source, int pos, int actionId) {
		switch (actionId) {
		case 111:
			mTypeNumber.setText(getActivity().getString(R.string.type_5_number));
			search.manufacture=1;
			pageNo=1;
			getData();
			break;
		case 112:
			mTypeNumber.setText(getActivity().getString(R.string.type_6_number));
			search.manufacture=2;
			pageNo=1;
			getData();
			break;
		default:
			super.onItemClick(source, pos, actionId);
			break;
		}
	}
	@Override
	public void onLoadMore() {
		super.onLoadMore();
		//getData();
	}
	private void getData(){
		if(mGetSongFromStore==null || mGetSongFromStore.getStatus()==Status.FINISHED){
			mGetSongFromStore=new GetSongFromStore();
			mGetSongFromStore.execute();
		}
	}
	private class UpdateSongLiked extends AsyncTask<SongDTO, Void, Void>{
		
		@Override
		protected Void doInBackground(SongDTO... params) {
			SongDTO song=params[0];
			boolean isLiked=(song.getLiked()>0?true:false);
			try {
				mSongDAO.open();
				mSongDAO.updateSongLiked(song.getId(), isLiked);
			} catch (Exception e) {
			}
			finally{
				mSongDAO.close();
			}
			return null;
		}
		
	}
	private class GetSongFromStore extends AsyncTask<Void, Void, List<SongDTO>>{
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			showNotFoundOrLoadingView(false, true);
			if(pageNo==1){
				mListView.setVisibility(View.GONE);
			}
		}
		@Override
		protected List<SongDTO> doInBackground(Void... arg0) {
			List<SongDTO> result=null;
			try {
				getListFaviorites();
				mSongDAO.open();
				//List<Integer> listIds=pagingFavioritesList(pageNo);
				result=mSongDAO.getListSongs(mActivity.getFavioriteIds());
			} catch (Exception e) {
				Log.i("SONGDAO OPEN", e.getMessage().toString());
			}
			finally{
				mSongDAO.close();
			}
			return result;
		}
		@Override
		protected void onPostExecute(List<SongDTO> result) {
			super.onPostExecute(result);
			if(result!=null && result.size()>0){
				if(pageNo==1){
					mAdapter.updateListData(result);
				}
				else{
					mAdapter.insertListData(result);
				}
				pageNo++;
			}
			if(mAdapter.getCount()<1){
				showNotFoundOrLoadingView(true, false);
				mListView.setVisibility(View.GONE);
			}else{
				showNotFoundOrLoadingView(false, false);
				mListView.setVisibility(View.VISIBLE);
			}
			mListView.onLoadMoreComplete();
		}		
	}
	@Override
	public void onResume() {
		super.onResume();
		pageNo=1;
		getData();
	}
	PopupWindow mpopup;
	public void setLiked(SongDTO song) {
		try {			
			DBKUtils.setUserUnLikedIds(mActivity.getFavioriteIds(), song.getId());								
			mAdapter.removeUnLikeSongs(song.getId());
			//mActivity.getFavioriteRemovedIds().add(song.getId());			
			if(mActivity.getFavioriteIds().size()>0){
				FavioritesDAO.getInstance().setFaviorites(mActivity.getFavioriteIds());
			}
			mActivity.getListSongAdapter().setUnLikeSongs(song.getId());
		} catch (Exception e) {
		}
	}
	public List<Integer> pagingFavioritesList(int page){
		List<Integer> result=null;
		try {
			result=mActivity.getFavioriteIds();
			if(page==0){
				page=1;
			}
			int from=AppConfig.OFFSET*(page-1);
			int to = AppConfig.OFFSET*page;
			if(to>result.size()){
				to=result.size();
			}
			result = result.subList(from, to);
		} catch (Exception e) {
			return null;
		}
		return result;
	}
}
